package com.ihr360.expr.operator;

import com.ihr360.expr.ExprBigDecimal;
import com.ihr360.expr.ExprDouble;
import com.ihr360.expr.ExprError;
import com.ihr360.expr.ExprException;
import com.ihr360.expr.core.AbstractMathematicalOperator;
import com.ihr360.expr.core.Expr;
import com.ihr360.expr.core.ExprType;

import java.math.BigDecimal;

/**
 * 四则运算乘法
 */
public class ExprMultiplication extends AbstractMathematicalOperator
{
    public ExprMultiplication(Expr lhs, Expr rhs) {
        super(ExprType.Multiplication, lhs, rhs);
    }

    @Override
    protected Expr evaluate(double lhs, double rhs) throws ExprException {
        return new ExprDouble(BigDecimal.valueOf(lhs).multiply(BigDecimal.valueOf(rhs)).doubleValue());
    }

    @Override
    protected Expr evaluate(BigDecimal lhs, BigDecimal rhs) throws ExprException {
        if (lhs==null || rhs==null){
            return ExprError.NULL;
        }
        return new ExprBigDecimal(lhs.multiply(rhs));
    }

    @Override
    public String toString() {
        return lhs + "*" + rhs;
    }
}